package cn.yaoud.salus.oms.alarm.feishu.bot;

import cn.hutool.http.HttpUtil;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.CardMsg;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.CardTemplateMsg;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.RichTextMsg;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.TextMsg;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.card.*;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.richtext.Post;
import cn.yaoud.salus.oms.alarm.feishu.bot.msg.richtext.RobotContent;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 飞书机器人
 *
 * @author 唐彪
 */
@Data
public class FeiShuRobot {
    private String name;
    private String webhook;

    /**
     * 发送文本消息
     * @param text 文本消息
     */
    public void info(String text) {
        TextMsg msg = new TextMsg(text);
        HttpUtil.post(webhook, JSON.toJSONString(msg));
    }
    /**
     * 发送富文本消息
     * <a href="https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot?lang=zh-CN#f62e72d5">发送富文本消息</a>
     *
     * @param content 富文本消息
     */
    public void info(RobotContent content) {
        Post post = new Post();
        RichTextMsg richTextMsg = new RichTextMsg(post);
        post.setZhCn(content);
        HttpUtil.post(webhook, JSON.toJSONString(richTextMsg));
    }

    /**
     * 发送卡片消息
     * <a href="https://open.feishu.cn/document/uAjLw4CM/ukzMukzMukzM/feishu-cards/quick-start/send-message-cards-with-custom-bot">
     * 向群自定义机器人请求发送飞书卡片</a>
     *
     * @param templateId       卡片ID
     * @param templateVersion  版本
     * @param templateVariable 模版变量
     */
    public void cardTemplate(String templateId, String templateVersion, Map<String, Object> templateVariable) {
        CardTemplateData data = new CardTemplateData(templateId, templateVersion, templateVariable);
        CardTemplate cardTemplate = new CardTemplate(data);
        CardTemplateMsg cardTemplateMsg = new CardTemplateMsg(cardTemplate);
        HttpUtil.post(webhook, JSON.toJSONString(cardTemplateMsg));
    }

    /**
     * 上报异常信息
     *
     * @param reportExceptionDto 异常信息
     */
    public void reportException(ReportExceptionDto reportExceptionDto) {
        Card card = new Card();
        CardMsg cardMsg = new CardMsg(card);

        CardHeader header = new CardHeader();
        card.setHeader(header);
        header.setTemplate(CardColorEnum.RED.getColor());
        CardTagContent cardTitle = new CardTagContent();
        cardTitle.setContent(reportExceptionDto.getTitle());
        cardTitle.setTag(ElementTagEnum.PLAIN_TEXT.getType());
        header.setTitle(cardTitle);

        List<CardElement> elements = new ArrayList<>();
        TagContentCardElement cardElement = new TagContentCardElement();
        cardElement.setTag(CardElementTagEnum.MARKDOWN.getType());
        cardElement.setContent("⏰ **告警时间：**" + reportExceptionDto.getTime());
        elements.add(cardElement);

        if (StringUtils.hasText(reportExceptionDto.getRequestUrl())) {
            TagContentCardElement urlCardElementElement = new TagContentCardElement();
            urlCardElementElement.setTag(CardElementTagEnum.MARKDOWN.getType());
            urlCardElementElement.setContent("ℹ️ **请求URL：**" + reportExceptionDto.getRequestUrl());
            elements.add(urlCardElementElement);
        }

        TagContentCardElement brief = new TagContentCardElement();
        brief.setTag(CardElementTagEnum.MARKDOWN.getType());
        brief.setContent("💡 **异常信息：**" + reportExceptionDto.getBrief());
        elements.add(brief);

        List<CardTagContent> cardTagContentList = new ArrayList<>();
        CollapsiblePanelCardElement collapsiblePanelCardElement = new CollapsiblePanelCardElement(cardTagContentList);
        CollapsiblePanelHeader collapsiblePanelHeader = new CollapsiblePanelHeader();
        CardTagContent cardTagContent = new CardTagContent();
        cardTagContent.setContent("**点击查看详情**");
        cardTagContent.setTag(ElementTagEnum.MARKDOWN.getType());
        collapsiblePanelHeader.setTitle(cardTagContent);
        collapsiblePanelHeader.setIcon(new CollapsiblePanelIcon());
        collapsiblePanelCardElement.setHeader(collapsiblePanelHeader);

        CardTagContent content = new CardTagContent();
        content.setTag(ElementTagEnum.MARKDOWN.getType());
        content.setContent(reportExceptionDto.getContent());
        cardTagContentList.add(content);
        collapsiblePanelCardElement.setElements(cardTagContentList);
        elements.add(collapsiblePanelCardElement);

        card.setElements(elements);
        HttpUtil.post(webhook, JSON.toJSONString(cardMsg));
    }
}
